package com.yf.imageview;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
	final int HTTP_SUCCESS = 0;
	final int HTTP_FAILURE = 1;
	final int HTTP_NETWORKLESS = 2;
	private EditText et_url;
	private Button btn_show;
	private ImageView iv_pic;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case HTTP_SUCCESS:
				iv_pic.setImageBitmap((Bitmap) msg.obj);
				Toast.makeText(MainActivity.this, "使用网络", Toast.LENGTH_SHORT).show();
				break;
			case HTTP_FAILURE:
				Toast.makeText(MainActivity.this, "网络连接", Toast.LENGTH_SHORT).show();
				break;
			case HTTP_NETWORKLESS:
				Toast.makeText(MainActivity.this, "服务器异常", Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		et_url = (EditText) findViewById(R.id.et_Url);
		btn_show = (Button) findViewById(R.id.btn_show);
		iv_pic = (ImageView) findViewById(R.id.iv_pic);
		
		et_url.setText("http://10.0.2.2/ic_launcher-web.png");

		btn_show.setOnClickListener(new OnClickListener() {

			final File file = new File(getCacheDir(), "ic_launcher-web.png");

			@Override
			public void onClick(View v) {
				if (file.exists()) {
					Log.v("btn_show", file.getAbsolutePath().toString());

					try {
						Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
						iv_pic.setImageBitmap(bitmap);
						Toast.makeText(MainActivity.this, "使用缓存", Toast.LENGTH_SHORT).show();
					} catch (Exception e) {
						e.printStackTrace();
						Message msg = Message.obtain();
						msg.what = HTTP_NETWORKLESS;
						handler.sendMessage(msg);
					}

					return;
				}

				new Thread(new Runnable() {

					@Override
					public void run() {
						String urlString = et_url.getText().toString().trim();
						try {
							URL url = new URL(urlString);
							HttpURLConnection conn = (HttpURLConnection) url.openConnection();
							conn.setRequestMethod("GET");
							conn.setConnectTimeout(8000);
							int resCode = conn.getResponseCode();

							if (resCode == 200) {
								InputStream is = conn.getInputStream();
								FileOutputStream os = new FileOutputStream(file);
								byte[] buf = new byte[1024];
								int len;
								while ((len = is.read(buf)) != -1) {
									os.write(buf, 0, len);
								}
								os.close();
								is.close();

								Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
								;
								Message msg = Message.obtain();
								msg.obj = bitmap;
								msg.what = HTTP_SUCCESS;
								handler.sendMessage(msg);

							} else {
								Message msg = Message.obtain();
								msg.what = HTTP_FAILURE;
								handler.sendMessage(msg);
							}

						} catch (Exception e) {
							e.printStackTrace();
							// 子线程不能修改UI
							// 方法一：子线程给主线程发送一个消息
							Message msg = Message.obtain();
							msg.what = HTTP_NETWORKLESS;
							handler.sendMessage(msg);
							// 方法二：使用runOnUiThread方法
							runOnUiThread(new Runnable() {

								@Override
								public void run() {
									Toast.makeText(MainActivity.this, "网络连接异常", Toast.LENGTH_SHORT)
											.show();
								}

							});
						}
					}
				}).start();
			}
		});
	}

}
